package com.orangelife.mobile.hourlyEmployee.adapter;

import com.curry.orangelife.mobile.R;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/** 钟点工服务时间的adapter */
public class SingleCheckedListAdapter extends BaseAdapter {

	private String[] mData;
	private String[] mFull;
	private Context mContext;
	private int isLeft;
	
	/**
	 * the View checked
	 */
	private TextView mLastCheckedView = null;
	/**
	 * the position in the data
	 */
	private int mCheckedPosition = -1;
	
	/** 把被点击的view的position放进来 */
	public void setCheckedPosition(int position){
		mCheckedPosition = position;
	}
	
	/** 加载数据 */
	public void setData(String[] data){
		mData = data;
		mLastCheckedView = null;
		mCheckedPosition = -1;
		notifyDataSetChanged();		
	}

	/** 把被点击的view放进来 */
	public void setCheckedView(View checkedView) {
		setViewSelected(mLastCheckedView, false);	
		TextView textView = (TextView)checkedView;
		setViewSelected(textView, true);		
	}	
	
	/** 使被点击的item的文字效果不会变没 */
	private void setViewSelected(TextView view, boolean selected){
		if(view != null){
			if(selected){
				/*SpannableStringBuilder style = new SpannableStringBuilder(view.getText());
				formerPosition = mCheckedPosition;
				if(isLeft == 0 && "1".equals(mFull[mCheckedPosition])) {
					style.setSpan(new ForegroundColorSpan(Color.parseColor("#F2530C")), 0, view.getText().length()-3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				} else if(isLeft == 0 && "0".equals(mFull[mCheckedPosition])) {
					style.setSpan(new ForegroundColorSpan(Color.parseColor("#F2530C")), 0, view.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				} else {
					style.setSpan(new ForegroundColorSpan(Color.parseColor("#F2530C")), 0, view.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				}
				view.setText(style);*/
				view.setTextColor(mContext.getResources().getColor(R.color.text_color_orange));
				mLastCheckedView = view;
			}else{
			/*	 //如果还有报错，这些就不用管它，因为加上这些，会再调用一次setViewSelected()，然后view就变成item之后第7个view
			 * SpannableStringBuilder style = new SpannableStringBuilder(view.getText());
				if(!StringUtil.isContainBlank(view.getText().toString())) {
					if(isLeft == 0 && "1".equals(mFull[formerPosition])) {
						style.setSpan(new ForegroundColorSpan(Color.parseColor("#000000")), 0, view.getText().length()-3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					} else if(isLeft == 0 && "0".equals(mFull[formerPosition])) {
						style.setSpan(new ForegroundColorSpan(Color.parseColor("#000000")), 0, view.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					} else {
						style.setSpan(new ForegroundColorSpan(Color.parseColor("#000000")), 0, view.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					}
				}
				view.setText(style);*/
				view.setTextColor(mContext.getResources().getColor(R.color.text_black));
			}
		}
	}

	public SingleCheckedListAdapter(Context context, String[] data, String[] mFull, int isLeft) {
		mContext = context;
		mData = data;
		this.isLeft = isLeft;
		this.mFull = mFull;
	}

	@Override
	public int getCount() {
		return mData.length;
	}

	@Override
	public Object getItem(int position) {
		return mData[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}	

	@Override
	public View getView(int position, View convertView, ViewGroup root) {		
		ViewHolder holder = null;
		if (convertView == null) {
			
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_hourly_service_time, null);			
			holder.name = (TextView) convertView.findViewById(R.id.textView1);
			convertView.setTag(holder);

		}else{
			
			holder = (ViewHolder) convertView.getTag();
			
		}
		/** 加这个判断是为了防止在没有选中的情况下，app停止 */
//		if(mCheckedPosition != -1) {
			setViewSelected(holder.name, mCheckedPosition == position);	
//		}
		
		/**
		 *  这个判断是为了让左边的listview里面的数据中，有（满）的话，（满）变成红色
		 *   isLeft：如果是0则是左边的listview, 1的话就是右边的listview
		 *   mFull则是存储对应数据是否是满，1是满，0是没满
	     */
//		if(isLeft == 0) {
//			if("1".equals(mFull[position])) {
//				SpannableStringBuilder style = new SpannableStringBuilder(mData[position]);
//				style.setSpan(new ForegroundColorSpan(Color.parseColor("#ff2b2b")), mData[position].length()-3, mData[position].length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//				holder.name.setText(style);
//			} else {
//				holder.name.setText(mData[position]);
//			}
//		} else {
			holder.name.setText(mData[position]);
//		}	
		return convertView;
	}	

	class ViewHolder{
		TextView name;
	}	

}
